//
//  InsurenceNormallCell.m
//  carcareIOS
//
//  Created by 王然 on 16/7/25.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "InsuranceNormallCell.h"

#define Head_Height (50)
#define Bottom_Height (74)
#define Line_Color COLOR_HEXSTRING(@"#EBEBEB")
#define Section_Height (10)
@interface InsuranceNormallCell()
//上半部分iconV
@property (nonatomic , strong) UIImageView * iconImageV;
//上半部分 title
@property (nonatomic , strong) UILabel * headTitleLabel;
//下半部分 标题左侧  驾驶平均分数
@property (nonatomic , strong) UILabel * bottomTitleLeftL;
//下半部分 标题右侧 保费未知
@property (nonatomic , strong) UILabel * bottomTitleRightL;
//下半部分 详情标题左侧 根据驾驶评分，给予特定的保费
@property (nonatomic , strong) UILabel * bottomDetailLeftL;
//下半部分 详情标题右侧 上传保单可知
@property (nonatomic , strong) UILabel * bottomDetailRightL;

@end

@implementation InsuranceNormallCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
     
        [[[[UIView viewWithFrame:CGRectMake(0, Head_Height + Bottom_Height - 0.5, __MAIN_WIDTH, Section_Height)] resetBackgroundColor:[Theme backGroundColor]] setupOnView:self] resetBorderWidth:0.5 borderColor:Line_Color];
    }
    return self;
}
- (void)createUI
{
    
    CGFloat labelAndImageX = 15 * __MAIN_RATIO_375;
    //创建上半部分cell
    CGFloat iconImageVW = 33 * __MAIN_RATIO_375;
    CGFloat imageViewY = (Head_Height - iconImageVW) / 2;
    self.iconImageV = [Factory imageViewWithFrame:CGRectMake(labelAndImageX , imageViewY, iconImageVW, iconImageVW) image:nil onView:self];
    self.iconImageV.contentMode = UIViewContentModeScaleToFill;
    self.iconImageV.clipsToBounds = YES;
    
//    UIImage * rightImage = ImageNamed(@"cell_right");
//    [Factory imageViewWithCenter: CGPointMake(__MAIN_WIDTH - 25, Head_Height / 2) image:rightImage onView:self];
    CGFloat titleLabelX = _iconImageV.right + 10 * __MAIN_RATIO_375;
//    CGFloat labelW = __MAIN_WIDTH - titleLabelX  - (rightImage.size.width + 28);
    CGFloat labelW = __MAIN_WIDTH - titleLabelX  - labelAndImageX;
    
    //优选优保
    self.headTitleLabel = [Factory labelWithFrame:CGRectMake(titleLabelX , 0, labelW , Head_Height) font:FONT_DEFAULT_Light(15) text:@"" textColor:[Theme wordColor] onView:self textAlignment:NSTextAlignmentLeft];
    self.headTitleLabel.backgroundColor = [UIColor clearColor];
    [[[UIView viewWithFrame:CGRectMake(labelAndImageX, Head_Height - 0.5, __MAIN_WIDTH - labelAndImageX * 2, 0.5)] resetBackgroundColor:Line_Color] setupOnView:self];
    
    
    // 创建下半部分cell
    CGFloat bottomTitleLY = 15 + Head_Height;
    CGFloat bottomRightLW = 100;
    CGFloat bottomLeftLW = __MAIN_WIDTH - labelAndImageX * 2 - bottomRightLW;
    CGFloat labelH = 20;
    self.bottomTitleLeftL = [[UILabel labelWithFrame:CGRectMake(labelAndImageX, bottomTitleLY, bottomLeftLW, labelH) font:FONT_DEFAULT_Light(14) text:@"" textColor:[Theme wordColor]] setupOnView:self];
    
    self.bottomTitleRightL = [[[[UILabel labelWithFrame:CGRectMake(self.bottomTitleLeftL.right, self.bottomTitleLeftL.top, bottomRightLW, labelH) font:FONT_DEFAULT_Light(23) text:@"" textColor:COLOR_HEXSTRING(@"#FF6716")]setupOnView:self]setupOnView:self] labelResetTextAlignment : NSTextAlignmentRight];
    
    self.bottomDetailLeftL = [[UILabel labelWithFrame:CGRectMake(labelAndImageX, self.bottomTitleRightL.bottom + 7, self.bottomTitleLeftL.width, labelH) font:FONT_DEFAULT_Light(12) text:@"" textColor:COLOR_HEXSTRING(@"#B0B0B0")] setupOnView:self];
    
    self.bottomDetailRightL = [[[UILabel labelWithFrame:CGRectMake(self.bottomTitleLeftL.right, self.bottomDetailLeftL.top, self.bottomTitleRightL.width, labelH) font:FONT_DEFAULT_Light(12) text:@"" textColor:COLOR_HEXSTRING(@"#B0B0B0")] setupOnView:self]  labelResetTextAlignment : NSTextAlignmentRight];

}
- (void)setModel:(InsurenceNorModel *)model
{
    _model = model;
    
    self.iconImageV.backgroundColor = [UIColor clearColor];
    [self.iconImageV setImageWithURL:[NSURL URLWithString:model.iconUrl]placeholderImage:ImageNamed(@"insureNorIconImage")];
    self.headTitleLabel.text = model.policyName;//@"优选优保"
    [self.bottomTitleLeftL setAttributedText:[self titleWithNameStr:[NSString stringWithFormat:@"%@ ",model.title] withUnitStr:[NSString stringWithFormat:@"%@",model.evaluation] withUnitFont:nil withUnitColor:COLOR_HEXSTRING(@"#FF6716")]];
    self.bottomDetailLeftL.text = model.message;
    
    
    NSString * rightTitleStr = [NSString stringWithFormat:@"%@",model.discount];
    if([rightTitleStr isNotNil])
    {
        NSArray * rightTitleArr = [rightTitleStr componentsSeparatedByString:@"."];
        self.bottomTitleRightL.font = FONT_DEFAULT_Light(23);
        if (rightTitleArr.count > 1) {
            [self.bottomTitleRightL setAttributedText:[self titleWithNameStr:rightTitleArr[0] withUnitStr:[NSString stringWithFormat:@".%@",rightTitleArr[1]] withUnitFont:FONT_DEFAULT_Light(15) withUnitColor:nil]];
        }else
        {
            NSArray * titleTextArr = [rightTitleStr componentsSeparatedByString:@"折"];
            if (titleTextArr.count > 1) {
                [self.bottomTitleRightL setAttributedText:[self titleWithNameStr:titleTextArr[0] withUnitStr:@"折" withUnitFont:FONT_DEFAULT_Light(15) withUnitColor:nil]];
            }else
            {
                self.bottomTitleRightL.text = rightTitleStr;
            }
        }
        self.bottomTitleRightL.top = Head_Height +  (Bottom_Height - self.bottomDetailLeftL.height) / 2;
        self.bottomDetailRightL.text = @"";
    }else
    {
        self.bottomTitleRightL.top = self.bottomTitleLeftL.top;
        self.bottomTitleRightL.font = FONT_DEFAULT_Light(15);
        self.bottomTitleRightL.text = model.discoutNoDataStr;
        self.bottomDetailRightL.text = model.discoutNoDataDetailStr;
    }
}
#pragma mark - 生成使用的attribute str
- (NSMutableAttributedString *)titleWithNameStr:(NSString *)nameStr withUnitStr:(NSString *)unitStr withUnitFont:(UIFont *)font withUnitColor:(UIColor *)color
{
    NSString * str = [NSString stringWithFormat:@"%@%@",nameStr, unitStr];
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
    if([unitStr length] > 0)
    {
        NSRange range = NSMakeRange([str length] - [unitStr length], [unitStr length]);
        if(font)[attributeStr addAttribute:NSFontAttributeName value:font range:range];
        if(color)[attributeStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
    return attributeStr;
}
+ (CGFloat)getCellHeight
{
    return Head_Height + Bottom_Height + Section_Height;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
